#include "qstatistic.h"
#include "qstatisticgraph.h"
#include "qanimal.h"

#include <iostream>
#include <cassert>


QStatistic::QStatistic ( int width, int height, QObject* parent ) : QGraphicsScene ( parent )
{
	aa = false;
	graphCount = -1;
	stepCount = 0;

	timeLine = new QGraphicsLineItem ( QLineF ( 0, 0, 0, sceneRect().height() ) );
	timeLine -> setPen ( QColor ( 120, 120, 120 ) );

	setSceneRect ( 0, 0, width, height );

	dimRect = addRect ( QRectF ( sceneRect() ) );

	addItem ( timeLine );
}

int QStatistic::addGraph ( QStatisticGraph* graph )
{
	graphCount++;
	
	addItem( graph );
	graphMap.insert ( graphCount, graph );

	return graphCount;
}

void QStatistic::pushPoint ( int graphId, QPointF point )
{
	if ( stepCount >= sceneRect().width() ) stepCount = 0;

	QStatisticGraph* graph = graphMap.value ( graphId );
	graph -> insertPoint ( point );

	stepCount = ( int ) point.x();
}

void QStatistic::reset()
{
	QList<QStatisticGraph*> values = graphMap.values();

	for ( int i = 0; i < values.size(); ++i )
	{
		QStatisticGraph* graph = values.at ( i );
		graph -> reset();
	}

	stepCount = 0;
}

void QStatistic::drawAntialiased ( bool aa )
{
	this -> aa = aa;
}

void QStatistic::stepAdvance()
{
	timeLine -> setLine ( QLineF ( stepCount, 0, stepCount, sceneRect().height() ) );
	dimRect -> setRect ( QRectF ( sceneRect() ) );
	advance();
}
